#!/usr/bin/env bash
# ******************************************************
# Filename     : mysql5.7_install.sh
# Last modified: 2022-12-14 12:18
# Version      :
# Author       : jack.zang
# Email        : jack_zang@126.com
# Description  :
# source <(curl -sL https://gitee.com/jack_zang/public-scripts/raw/master/shell/mysql/mysql5.7_install.sh)
#
# ******************************************************

MYSQL_DATADIR="/data/mysql/data"

## 安装 mysql 5.7 仓库源
rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum repolist all | grep mysql | grep enabled

# 解决 GPG keys 问题
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

yum -y install mysql-community-server

cat > /etc/my.cnf <<EOF
[mysqld]
datadir=${MYSQL_DATADIR}
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF

systemctl start mysqld && systemctl enable mysqld && systemctl status mysqld

cat /var/log/mysqld.log |grep -i "temporary password"

# 执行安全配置
mysql_secure_installation

# mysql 开启远程登录
mysql -uroot -p
use mysql;
GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'Xiodi.cn123' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

# 显示所有用户
use mysql;
select `user`,authentication_string,`Host` from `user`;